//
//  VisitorView.swift
//  Weibo1.0
//
//  Created by lianghuahao on 15/12/9.
//  Copyright © 2015年 lianghuahao.com. All rights reserved.
//  协议  代理  监听方法

import UIKit

//protocol VisitorViewDelegate: NSObjectProtocol {
//    //注册
//    func VisitorViewDidRegister()
//    //登陆
//    func VisitorViewDidLogin()
//
//}

/// 访客视图--处理用户未登录界面显示
class VisitorView: UIView {
//    /// 代理
//    weak var delegate: VisitorViewDelegate?
//    // MARK: - 协议方法
//    @objc private func clickLogin() {
//        delegate?.VisitorViewDidLogin()
//    }
//    @objc private func clickRegister() {
//        delegate?.VisitorViewDidRegister()
//    }
    
    
    
    // MARK: - 设置访客视图信息
    /// 设置访客视图信息
    ///
    /// - parameter imageName: 图片名称
    /// - parameter title:     消息文字
    func setupInfo(imageName: String?, title: String) {
        messageLable.text = title
        //如果图片名称为nil, 说明是首页, 直接返回
        guard let imgName = imageName else{
            startAnim()
            return
        }
        
        iconView.image = UIImage(named: imgName)
        
        //隐藏小房子
        homeIconView.hidden = true
        //将遮罩图像移动到底层
        sendSubviewToBack(maskIconView)
    }
    
    /// 开启首页转轮动画
    private func startAnim() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //removedOnCompletion用在不断重复的动画上, 当动画绑定的图层对应的视图被销毁, 动画会自动被销毁
        anim.removedOnCompletion = false
        // 添加到图层
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    // 构造函数
    //initWithFrame 是 UIView 的置顶构造函数
    //使用纯代码开发使用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    //initWithCode - 使用SB 或者Xib 开发加载的函数
    //使用 SB 开始的入口,,SB就是一个XML文件,编译之后会变成二进制文件, 当我们二进制一加载之后,这时候我们从SB界面画的所有东西都有了
    required init?(coder aDecoder: NSCoder) {
        
        //如果使用SB开发,调用这个视图,就会直接崩溃
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    //MARK: - 懒加载控件
    /// 图标, 使用 image: 构造函数创建的 imageView 默认就是 image 的大小
    private lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    /// 遮罩图像
    private lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")

    /// 小房子
    private lazy var homeIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    /// 消息文字
    private lazy var messageLable: UILabel = UILabel(title: "关注一些人,回这里看看有什么惊喜关注一些人,回这里看看有什么惊喜")

    /// 注册按钮
    
    lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), imageName: "common_button_white_disable")
    /// 登陆按钮
    lazy var loginButton: UIButton = UIButton(title: "登录", color: UIColor.darkGrayColor(), imageName: "common_button_white_disable")
    
}
// MARK: - 设置界面
extension VisitorView {

    ///设置界面
    private func setupUI() {
        //1. 添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLable)
        addSubview(registerButton)
        addSubview(loginButton)
        //2. 设置自动布局
        /**
            添加约束需要 添加到父视图上
            建议:  子视图有一个同一的参照物
        */
        //translatesAutoresizingMaskIntoConstraints默认是true, 支持使用setFrame的方式设置控件位置
        //false 支持使用自动布局来设置
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        //小房子
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //消息文字
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: 50
//            ))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLable, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //登陆按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLable, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //遮罩图像
        /**
            VFL  :  可视化格式语言
            H : 水平方向
            V : 垂直方向
            | : 边界
            []: 包装控件
            views: 是一个字典 [名字: 控件名] - VFL 字符串中表示控件的字符串
            metrics: 是一个字典 [名字: NSNumber]- VFL 字符串中表示某个数值
        */
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": maskIconView]))
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight": -36], views: ["mask": maskIconView, "regButton": registerButton]))
        //设置背景颜色 - 灰度图 R = G = B 在 UI 元素中, 大多数都是用灰度图,  或者纯色图(安全色)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
//    //添加代理监听方法
//        registerButton.addTarget(self, action: "clickRegister", forControlEvents: .TouchUpInside)
//        loginButton.addTarget(self, action: "clickLogin", forControlEvents: .TouchUpInside)
    }
}





















